/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-7 下午2:23
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter.live;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.live.LiveCommon;
import com.example.dqhl.jdmetro.config.Config;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
*description:直播公共Adapter
*autour:guanluocang
*date:17/7/13 09:22
*/
public class LiveCommonAdapter extends RecyclerView.Adapter<LiveCommonAdapter.LiveDataViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<LiveCommon> mCommonList = new ArrayList<>();
    private onItemClickListener onItemClickListener;

    public LiveCommonAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public void refresh_list(List<LiveCommon> commonList) {
        this.mCommonList = commonList;
        notifyDataSetChanged();
    }
    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

    }
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public LiveDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LiveDataViewHolder liveDataViewHolder = new LiveDataViewHolder(mInflater.inflate(R.layout.layout_live_data, parent, false));
        return liveDataViewHolder;
    }

    @Override
    public void onBindViewHolder(final LiveDataViewHolder holder, int position) {
        holder.tv_Text.setText(mCommonList.get(position).getTitle());
        Picasso.with(mContext).load(Config.pic_base_url + mCommonList.get(position).getPic()).into(holder.iv_liveCommonPic);
//        holder.iv_liveCommonPic.setImageURI(Uri.parse(Config.pic_base_url + mCommonList.get(position).getPic()));
//        x.image().bind(holder.iv_liveCommonPic, Config.pic_base_url + mCommonList.get(position).getPic());
        if(onItemClickListener != null){
            holder.ll_liveItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mCommonList.size();
    }

    class LiveDataViewHolder extends RecyclerView.ViewHolder {
        TextView tv_Text;
        ImageView iv_liveCommonPic;
        RelativeLayout ll_liveItem;

        public LiveDataViewHolder(View view) {
            super(view);
            (tv_Text) = (TextView) view.findViewById(R.id.tv_Text);
            (iv_liveCommonPic) = (ImageView) view.findViewById(R.id.iv_liveCommonPic);
            (ll_liveItem) = (RelativeLayout) view.findViewById(R.id.ll_liveItem);

        }
    }
}
